//Calendario
jQuery(function($){
	$.datepicker.regional['it'] = {
		closeText: 'Chiudi',
		prevText: '&#x3c;Prec',
		nextText: 'Succ&#x3e;',
		currentText: 'Oggi',
		monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno',
		             'Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'],
		monthNamesShort: ['Gen','Feb','Mar','Apr','Mag','Giu',
			'Lug','Ago','Set','Ott','Nov','Dic'],
		dayNames: ['Domenica','Luned&#236','Marted&#236','Mercoled&#236','Gioved&#236','Venerd&#236','Sabato'],
		dayNamesShort: ['Dom','Lun','Mar','Mer','Gio','Ven','Sab'],
		dayNamesMin: ['Do','Lu','Ma','Me','Gi','Ve','Sa'],
		weekHeader: 'Sm',
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['it']);
});

var selezionato;
$(".ui-datepicker-title").live("click", function() {
	selezionato.value="prova";
    var myMonth = $(this).find(".ui-datepicker-month").html();
    var myYear = $(this).find(".ui-datepicker-year").html();
    selezionato.value=myMonth+" "+myYear;
    $(this).parents().find(".hasDatepicker").datepicker( "hide" );
        }
    );

$(function() {
	$( "#datap" ).datepicker({
		minDate: "+1D", 
		maxDate: "+1Y",
		onClose: function(dateText, inst) {
			if (this.value.split("-").length>1){ 
			$( "#datar" ).datepicker('change',{ 
	            minDate: this.value
	        });
			}
			else $( "#datar" ).datepicker('change',{ minDate: "+1D"});
		}});
	$( "#datar" ).datepicker({
		minDate: "+1D",
		maxDate: "+1Y",
		onClose: function(dateText, inst) {
			if (this.value.split("-").length>1){ 
			$( "#datap" ).datepicker('change',{ 
	            maxDate: this.value
	        });
			}
		}});
	
	
//Suggerimenti luoghi
	var cache = {}, 
	lastXhr=null;
	$( "#departure" ).autocomplete({
		minLength: 1,
		source: function( request, response ) {
			var term = request.term;
			if ( term in cache ) {
				response( cache[ term ] );
				return;
			}

			lastXhr = $.getJSON( "autocomplete.jsp", request, function( data, status, xhr ) {
				cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
				}
			});
		}
	})
	.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.desc + "</a>" )
			.appendTo( ul );
	};
	$( "#arrival" ).autocomplete({
		minLength: 1,
		source: function( request, response ) {
			var term = request.term;
			if ( term in cache ) {
				response( cache[ term ] );
				return;
			}

			lastXhr = $.getJSON( "autocomplete.jsp", request, function( data, status, xhr ) {
				cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
				}
			});
		}
	})
	.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.desc + "</a>" )
			.appendTo( ul );
	};
});